/**
 * 
 */
package org.gov.vis.data.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.data.controller.ProgencyCtlr;
import org.gov.vis.data.controller.ProgencyCtlrImpl;
import org.gov.vis.data.dto.ProgencyDto;
import org.gov.vis.data.gui.AddProgencyDialog;
import org.gov.vis.data.gui.EditProgencyDialog;
import org.gov.vis.data.gui.ViewProgencyDialog;
import org.gov.vis.gui.util.ErrorsDisplayJPanel;
import org.gov.vis.main.Main;
import org.gov.vis.master.gui.listener.BaseListener;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.Util;

/**
 * @author user
 *
 */
public class EditProgencyListener implements ActionListener,BaseListener{




	private EditProgencyDialog editProgencyDialog;
	private ProgencyCtlr progencyCtlr = new ProgencyCtlrImpl();

	private ErrorsDisplayJPanel errorMsgPanel;
	final private ResourceBundle rb = Util.getResources();
	private ProgencyDto progencyDto;

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */

	public EditProgencyListener(EditProgencyDialog progencyDialog){

		this.editProgencyDialog = progencyDialog;
	}
	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		try{
			//errorMsgPanel = new ErrorsDisplayJPanel(editProgencyDialog.getjPanel1());
			//errorMsgPanel.clearMsgPanel();
			if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_update"))){
				//Method called to either Add or Edit the GopalaMithra detprogencyls
				addOrEditProgency();
			}

			if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_delete"))){
				
				int response = JOptionPane.showConfirmDialog(null, GuiConstants.DELETE_CONFIRM_MSG, "Confirm",
				        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
				    
			    if (response == JOptionPane.YES_OPTION) {
			      // User clicked YES.
			    	deleteProgency(actionEvent);
			    	String searchText = editProgencyDialog.getSearchText();

					// It is basically doing back button functionality. Please, understand the logic and write the comments.
					//Replace double quotes with appropriate Dialog title.
					ViewProgencyDialog viewProgenDialog = new ViewProgencyDialog(Main.getMainFrame(), "View Progency", false);
					for(ActionListener actionListener: viewProgenDialog.getSearchBtn().getActionListeners()) { 
						ViewProgencyListener viewProgenList = (ViewProgencyListener)actionListener;
						viewProgenDialog.getCodeTxtBx().setText(searchText);

						viewProgenList.actionPerformed(new ActionEvent(viewProgenDialog.getSearchBtn(), 1, rb.getString("dialog_button_serach")));
					}

			    	
			    } else if (response == JOptionPane.NO_OPTION || response == JOptionPane.CLOSED_OPTION) {
			      return;
			    }

			}

		}catch(Exception e){
			e.printStackTrace();
		}
	}//end actionPerformed

	/**
	 * Method called to delete the Progency Object based on the passed progencyId
	 * @param _progencyId
	 */
	private void deleteProgency(ActionEvent actionEvent){

		Long _progencyId = (Long)editProgencyDialog.getHidnObject();
		try {
			progencyCtlr.deleteProgency(_progencyId);
			JOptionPane.showMessageDialog(editProgencyDialog, rb.getString(GuiConstants.DATA_DELETEPROGENCY_SUCCESS));
			editProgencyDialog.dispose();

			
		} catch (Exception e) {
			List<String> errorMsgLst = new ArrayList<String>();
			errorMsgLst.add(e.getMessage());
			errorMsgPanel.setErrorMessages(errorMsgLst);


		}

	}//end deleteProgency


	/**
	 * Method called to either Add or Edit the Progency detprogencyls
	 */
	private void addOrEditProgency(){
		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);

		//Method to set the Screen's data to  the DTO Object
		setGuiDetailsToDto(editProgencyDialog);
		try{
			//Method called to Add or Edit the Progency
			progencyCtlr.addOrEditProgency(progencyDto);
			JOptionPane.showMessageDialog(editProgencyDialog, rb.getString(GuiConstants.DATA_EDITPROGENCY_SUCCESS));
			editProgencyDialog.dispose();
			
			String searchText = editProgencyDialog.getSearchText();

			// It is basically doing back button functionality. Please, understand the logic and write the comments.
			//Replace double quotes with appropriate Dialog title.
			ViewProgencyDialog viewProgenDialog = new ViewProgencyDialog(Main.getMainFrame(), "View Progency", false);
			for(ActionListener actionListener: viewProgenDialog.getSearchBtn().getActionListeners()) { 
				ViewProgencyListener viewProgenList = (ViewProgencyListener)actionListener;
				viewProgenDialog.getCodeTxtBx().setText(searchText);

				viewProgenList.actionPerformed(new ActionEvent(viewProgenDialog.getSearchBtn(), 1, rb.getString("dialog_button_serach")));
			}

		}catch(Exception e){
			List<String> errorMsgLst = new ArrayList<String>();
			if(e instanceof InvalidInputException){
				final HashMap<String,String> _exceptions = ProgencyCtlr.errorMsgs;
				if (_exceptions != null && !_exceptions.isEmpty()) {
					String errMsg = null;

					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_code").toUpperCase())){
						editProgencyDialog.getLblCode().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_code").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_frmcode").toUpperCase())){
						editProgencyDialog.getLblFarmerCode().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_frmcode").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_frmname").toUpperCase())){
						editProgencyDialog.getLblFarmerName().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_frmname").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_nocalving").toUpperCase())){
						editProgencyDialog.getLblNoOfCalving().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_nocalving").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_noaidoneconception").toUpperCase())){
						editProgencyDialog.getLblNoAIDoneConc().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_noaidoneconception").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_prntg").toUpperCase())){
						editProgencyDialog.getLblPercentage().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_prntg").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_prvlctyldltrs").toUpperCase())){
						editProgencyDialog.getLblPrevVacYldLtrs().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_prvlctyldltrs").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_prtmlkyeldpday").toUpperCase())){
						editProgencyDialog.getLblPrsntMlkYldLtrs().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_prtmlkyeldpday").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					/*if(_exceptions.containsKey(rb.getString("dialog_label_prgy_grwmnthwgt").toUpperCase())){
						editProgencyDialog.getLblw.setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_grwmnthwgt").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_grwbrthwgt").toUpperCase())){
						editProgencyDialog.getLblBrwBrtWgt().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_grwbrthwgt").toUpperCase());
						errorMsgLst.add(errMsg);
					}
*/					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_dmmilk").toUpperCase())){
						//editProgencyDialog.getLbl.setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_dmmilk").toUpperCase());
						errorMsgLst.add(errMsg);
					}

				//	errorMsgPanel.setErrorMessages(errorMsgLst);					
				} else {
					//log.debug(e.getMessage(), e);
				}	
			}
			else{
				errorMsgLst.add(e.getMessage());
				errorMsgPanel.setErrorMessages(errorMsgLst);

			}
		}		
	}//end addOrEditProgency

	/**
	 * Method called to Clear all the form fields of the screen
	 */
	public void clear() {//set the Field's data to empty values
		editProgencyDialog.getTxtFldCode().setText("");
		//editProgencyDialog.getTxtFldG.setText("");
		editProgencyDialog.getTxtBxBreed().setText("");
		editProgencyDialog.getTxtBxBreedType().setText("");
		editProgencyDialog.getTxtBxBullNo().setText("");
		editProgencyDialog.getDtChoCalfDateOfBirth().setDate(null);
		editProgencyDialog.getTxtBxColor().setText("");
		editProgencyDialog.getTxtBxDistrict().setText("");
		editProgencyDialog.getTxtBxFarmerCode().setText("");
		editProgencyDialog.getTxtBxFarmerName().setText("");
		editProgencyDialog.getTxtBxMandal().setText("");
		//editProgencyDialog.getTxtFldGrwmthWgt().setText("");
		editProgencyDialog.getTxtNoOfAiDoneConc().setText("");
		editProgencyDialog.getTxtBxNoCalving().setText("");
		editProgencyDialog.getTxtBxPercentage().setText("");
		editProgencyDialog.getTxtBxPrsntMilkYldLtrs().setText("");
		editProgencyDialog.getTxtBxPrvVactYldLtrs().setText("");
		editProgencyDialog.getTxtBxRelationType().setText("");
		//editProgencyDialog.getCombxSexType().setSelectedIndex(0);
		editProgencyDialog.getTxtBxTagNo().setText("");
		editProgencyDialog.getTxtBxVillage().setText("");
		//editProgencyDialog.getDtChoDateOfDeworm.setDate(null);
		//editProgencyDialog.getDtChoDateOfDeticking().setDate(null);
		//editProgencyDialog.getDtChoDateOfVaccination().setDate(null);

		//Method called to set the Field's
		
		//Labels to default Black color
		clearGUILabelColors(blackColor);


		//ResultPanel
		//	progencyDialog.getLblResultOrg().setText("");
		//progencyDialog.getLblrRsultCode().setText("");

}//end clear

	/**
	 * Method called to read the data from GUI and set them to DTO Object 
	 * @param _guiObject
	 */
	@Override
	public void setDtoDetailsToGui(Object object) {
		// TODO Auto-generated method stub
		final ProgencyDto _progencyDto = (ProgencyDto)object;
		editProgencyDialog.getTxtFldCode().setText(_progencyDto.getCode());
		//editProgencyDialog.getTxtFldGrwBtWgt().setText(_progencyDto.getBirthWeight());
		editProgencyDialog.getTxtBxBreed().setText(_progencyDto.getBreed());
		editProgencyDialog.getTxtBxBreedType().setText(_progencyDto.getBreedType());
		editProgencyDialog.getTxtBxBullNo().setText(_progencyDto.getBullNo());
		//editProgencyDialog.getTxtFldClfDob().setText(DU.format(_progencyDto.getClfDOB(),DBConstants.DISPLAY_DATE_FORMAT));
		editProgencyDialog.getDtChoCalfDateOfBirth().setDate(progencyDto.getClfDOB());
		editProgencyDialog.getTxtBxColor().setText(_progencyDto.getColor());
		editProgencyDialog.getTxtBxDistrict().setText(_progencyDto.getDistrict());
		editProgencyDialog.getTxtBxFarmerCode().setText(_progencyDto.getFarmerCode());
		editProgencyDialog.getTxtBxFarmerName().setText(_progencyDto.getFarmerName());
		editProgencyDialog.getTxtBxMandal().setText(_progencyDto.getMandal());
		//editProgencyDialog.getTxtFldGrwmthWgt().setText(_progencyDto.getMonthlyWeight());
		editProgencyDialog.getTxtNoOfAiDoneConc().setText(_progencyDto.getNoOfAI());
		editProgencyDialog.getTxtBxNoCalving().setText(_progencyDto.getNoOfCalving());
		editProgencyDialog.getTxtBxPercentage().setText(_progencyDto.getPercentage());
		editProgencyDialog.getTxtBxPrsntMilkYldLtrs().setText(_progencyDto.getPresentYield());
		editProgencyDialog.getTxtBxPrvVactYldLtrs().setText(_progencyDto.getPreviousYield());
		editProgencyDialog.getTxtBxRelationType().setText(_progencyDto.getRelationShipName());
		editProgencyDialog.getTxtBxSex().setText(_progencyDto.getSex());
		editProgencyDialog.getTxtBxTagNo().setText(_progencyDto.getTagNo());
		editProgencyDialog.getTxtBxVillage().setText(_progencyDto.getVillage());

	}

	/**
	 * Method called to read the data from DTO and set them to GUI.
	 * @param _guiObject
	 */
	@Override
	public void setGuiDetailsToDto(Object object) {
		// TODO Auto-generated method stub
		final EditProgencyDialog _progencyDialog = (EditProgencyDialog)object;
		progencyDto = new ProgencyDto();
		progencyDto.setCode(_progencyDialog.getTxtFldCode().getText());
		//progencyDto.setBirthWeight(_progencyDialog.getTxtFldGrwBrtWgt().getText());
		progencyDto.setBreed(_progencyDialog.getTxtBxBreed().getText());
		progencyDto.setBreedType(_progencyDialog.getTxtBxBreedType().getText());
		progencyDto.setBullNo(_progencyDialog.getTxtBxBullNo().getText());
		//progencyDto.setClfDOB(_progencyDialog.getTxtFldCalfDOB().getText());
		if(_progencyDialog.getDtChoCalfDateOfBirth().getDate() != null){
			progencyDto.setClfDOB(_progencyDialog.getDtChoCalfDateOfBirth().getDate());
		}
		progencyDto.setColor(_progencyDialog.getTxtBxColor().getText());
		progencyDto.setDistrict(_progencyDialog.getTxtBxDistrict().getText());
		progencyDto.setFarmerCode(_progencyDialog.getTxtBxFarmerCode().getText());
		progencyDto.setFarmerName(_progencyDialog.getTxtBxFarmerName().getText());
		progencyDto.setMandal(_progencyDialog.getTxtBxMandal().getText());
		//progencyDto.setMonthlyWeight(_progencyDialog.getTxtFldGrwmthWgt().getText());
		progencyDto.setNoOfAI(_progencyDialog.getTxtNoOfAiDoneConc().getText());
		progencyDto.setNoOfCalving(_progencyDialog.getTxtBxNoCalving().getText());
		progencyDto.setPercentage(_progencyDialog.getTxtBxPercentage().getText());
		progencyDto.setPresentYield(_progencyDialog.getTxtBxPrsntMilkYldLtrs().getText());
		progencyDto.setPreviousYield(_progencyDialog.getTxtBxPrvVactYldLtrs().getText());
		progencyDto.setRelationShipName(_progencyDialog.getTxtBxRelationType().getText());
		progencyDto.setSex(String.valueOf(_progencyDialog.getVisComBoxModel().getSelectedKey()));
		progencyDto.setTagNo(_progencyDialog.getTxtBxTagNo().getText());
		progencyDto.setVillage(_progencyDialog.getTxtBxVillage().getText());
		progencyDto.setPrgyId((Long)editProgencyDialog.getHidnObject());
		progencyDto.setDmMilkYield(_progencyDialog.getTxtBxSiresMilkYieldLtrs().getText());
		progencyDto.setSirenBreed(_progencyDialog.getJTextField1().getText());
		//progencyDto.setFollowUpDetails(AddProgencyDialog.tempFollowUpLst);
		progencyDto.setFollowUpDetails(EditProgencyDialog.tempFollowUpLst);
		
		

	}


	public void setProgencyCtlr(ProgencyCtlr progencyCtlr) {
		this.progencyCtlr = progencyCtlr;
	}

	public ProgencyCtlr getProgencyCtlr() {
		return progencyCtlr;
	}
	@Override
	public void clearGUILabelColors(Color _lblColor) {//set the Field's Labels to default Black color
		editProgencyDialog.getLblCode().setForeground(_lblColor);
		editProgencyDialog.getLblBreed().setForeground(_lblColor);
		editProgencyDialog.getLblBreedType().setForeground(_lblColor);
		editProgencyDialog.getLblBullNo().setForeground(_lblColor);
		editProgencyDialog.getLblCalfDob().setForeground(_lblColor);
		editProgencyDialog.getLblMandal().setForeground(_lblColor);
		editProgencyDialog.getLblColor().setForeground(_lblColor);
		//editProgencyDialog.getLblDeticking().setForeground(_lblColor);
		//editProgencyDialog.getLblDeWorming().setForeground(_lblColor);
		editProgencyDialog.getLblDistrict().setForeground(_lblColor);
		editProgencyDialog.getLblFarmerCode().setForeground(_lblColor);
		editProgencyDialog.getLblFarmerName().setForeground(_lblColor);
	//	editProgencyDialog.getLblBrwBrtWgt().setForeground(_lblColor);
		//editProgencyDialog.getLblGrwMthWgt().setForeground(_lblColor);
		editProgencyDialog.getLblNoAIDoneConc().setForeground(_lblColor);
		editProgencyDialog.getLblNoOfCalving().setForeground(_lblColor);
		//editProgencyDialog.getLblNutrition().setForeground(_lblColor);
		//editProgencyDialog.getLblOthr().setForeground(_lblColor);
		editProgencyDialog.getLblPercentage().setForeground(_lblColor);
		editProgencyDialog.getLblPrevVacYldLtrs().setForeground(_lblColor);
		editProgencyDialog.getLblPrsntMlkYldLtrs().setForeground(_lblColor);
		editProgencyDialog.getLblRelationtype().setForeground(_lblColor);
		editProgencyDialog.getLblSex().setForeground(_lblColor);
		editProgencyDialog.getLblTagNo().setForeground(_lblColor);
		//editProgencyDialog.getLblVaccination().setForeground(_lblColor);
		editProgencyDialog.getLblVillage().setForeground(_lblColor);

}
	
	



}//end class
